package me.ingxin.android.tools.app.sysbar

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_STOP
import androidx.core.view.updatePadding
import me.ingxin.android.tools.app.utils.logD
import me.ingxin.android.sysbar.*
import me.ingxin.android.tools.app.databinding.ActivitySysBarBinding
import me.ingxin.android.tools.singleClick
import kotlin.math.min

/**
 * Created by ingxin on 2022/5/24
 */
class SysBarActivity : AppCompatActivity() {

    private val binding by lazy { ActivitySysBarBinding.inflate(layoutInflater) }
    var isFull = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        supportActionBar?.hide()

        //允许全屏
        window.allowShortEdges()

        //沉浸式
        window.immersion()
        window.statusBarColor = Color.BLUE
        window.navigationBarColor = Color.TRANSPARENT

        binding.btn.singleClick {
            if (isFull) {
                window.exitFullScreen()
                //退出全屏后,需要重新启动沉浸式
                window.immersion()
            } else {
                window.fullScreen()
            }
            isFull = !isFull
        }

        binding.btnDialog.singleClick {
            SysBarDialog(this).show()
        }

        InsetsWatcher(binding.tv)
            .onSystemBarsHeightCallback {
                logD("状态栏高${it.first},导航栏高${it.second}")
                //留出状态栏
                binding.tv.updatePadding(top = it.first)
                //留出导航栏距离
                binding.root.updatePadding(bottom = it.second)
            }
            .onIMEAnimationCallback(binding.et, DISPATCH_MODE_STOP) {
                logD("键盘顶部距离底部$it")
                val navH = binding.et.getSystemBarsHeight().second
                val d = min(navH - it, 0)
                binding.et.translationY = d.toFloat()
            }
            .launch()
    }

    override fun onResume() {
        super.onResume()
        if (isFull) {
            window.fullScreen()
        }
    }

    override fun onPause() {
        window.hideIME()
        super.onPause()
    }

}